package Java攻略._2_java_util_function包._4_Function接口;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class _13_将字符串映射到它们的长度 {

    public static void main(String[] args) {
        List<String> names = Arrays.asList("Mal", "Wash", "Kaylee", "Inara",
                "Zoë", "Jayne", "Simon", "River", "Shepherd Book");

        // 匿名内部类
        List<Integer> nameLength = names.stream()
                .map(new Function<String, Integer>() {

                    @Override
                    public Integer apply(String s) {
                        return s.length();
                    }
                })
                .collect(Collectors.toList());

        System.out.println(nameLength);


        // lambda表达式
        nameLength = names.stream()
                .map(x -> x.length())
                .collect(Collectors.toList());
        System.out.println(nameLength);

        // 方法引用
        nameLength = names.stream()
                .map(String::length)
                .collect(Collectors.toList());
        System.out.println(nameLength);
    }
}












